package com.beyond.market.cart.controller;

import com.beyond.market.cart.domain.Cart;
import com.beyond.market.cart.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.SQLException;
import java.util.Map;

@Controller
public class CartController1 {
    @Autowired
    private CartService cartService;

    @RequestMapping("/removeCart")
    public String remove(Integer id){
        try {
            int remove = cartService.remove(id);
            if (remove == 1){
                return "ok";
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }
    @RequestMapping("/getByIdCart")
    public String getById(Integer id, Map<String, Object> map) {

        try {
           Cart c = cartService.getById(id);
            map.put("cart",c);
            return "cart";
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/save")
    @ResponseBody
    public String save() {
        Cart carts = new Cart();
        carts.setCartId(1);
        carts.setUserId(1);
        carts.setBuyNum("100");

        try {

            Integer save = cartService.save(carts);
            if(save == 1) {
                return "index";
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}

